HAL OPAMP initialization and configuration functions migration ¶
The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:
Open STM32CubeMX2 OPAMP configuration panel.
Configure the SW layer HAL.
Configure the OPAMP main features.
Configure the additional OPAMP features if needed.
Configure the OPAMP GPIO pins.
Configure the NVIC if the application requires the OPAMP to be used in IT mode.
Generate the code.
HAL OPAMP configuration panel ¶
Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.
HAL OPAMP main features configuration ¶
Configure the OPAMP main features.
HAL OPAMP additional features configuration ¶
Configure the additional OPAMP features if needed.
GPIO configuration ¶
Configure GPIO pins.
Generated OPAMP initialization and de-initialization sequences ¶
The OPAMP initialization and de-initialization sequences are generated in the
mx_opamp_hal_init
function in
mx_opamp.c
file under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
Related concepts:
Default configuration :
|
Topic |
HAL1 code snippet |
HAL2 code snippet |
|---|---|---|
|
OPAMP Init, config and Msp sequences |
void MX_OPAMP1_Init(void)
{
hopamp1.Instance = OPAMP1;
hopamp1.Init.Mode = OPAMP_STANDALONE_MODE;
hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
hopamp1.Init.InvertingInput = OPAMP_INVERTINGINPUT_IO0;
hopamp1.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* hopamp)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hopamp->Instance==OPAMP1)
{
/* USER CODE BEGIN OPAMP1_MspInit 0 */
/* USER CODE END OPAMP1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_OPAMP_CLK_ENABLE();
/* USER CODE BEGIN OPAMP1_MspInit 1 */
/* USER CODE END OPAMP1_MspInit 1 */
}
}
|
hal_opamp_handle_t *mx_opamp1_init(void)
{
HAL_RCC_OPAMP1_EnableClock();
/****************************************************************************/
/* Configuration of basic features (mandatory) */
/****************************************************************************/
hal_opamp_config_t opamp_config;
hal_opamp_config_input_connection_t config_input_connection;
if (HAL_OPAMP_Init(&hOPAMP1, HAL_OPAMP1) != HAL_OK)
{
return NULL;
}
opamp_config.speed_mode = HAL_OPAMP_SPEED_MODE_NORMAL;
opamp_config.configuration_mode = HAL_OPAMP_MODE_STANDALONE;
opamp_config.opamp_output = HAL_OPAMP_OUTPUT_CONNECTION_EXTERNAL;
HAL_OPAMP_SetConfig(&hOPAMP1, &opamp_config);
config_input_connection.inverting_input = HAL_OPAMP_INVERTING_INPUT_GPIO_0;
config_input_connection.non_inverting_input = HAL_OPAMP_NON_INVERTING_INPUT_GPIO_0;
HAL_OPAMP_SetConfigInputConnection(&hOPAMP1, &config_input_connection);
HAL_OPAMP_SetPGAExternalMode(&hOPAMP1, HAL_OPAMP_PGA_EXT_NONE);
HAL_RCC_GPIOA_EnableClock();
hal_gpio_config_t gpio_config;
/*
OPAMP1 GPIO Configuration
[GPIO Pin] ------> [Signal Name]
PA1 ------> OPAMP1_VINP0
PA6 ------> OPAMP1_VOUT
PA2 ------> OPAMP1_VINM0
*/
gpio_config.mode = HAL_GPIO_MODE_ANALOG;
gpio_config.pull = HAL_GPIO_PULL_NO;
HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_6 | HAL_GPIO_PIN_2, &gpio_config);
return &hOPAMP1;
}
|
|
OPAMP Deinit sequence |
static void MX_OPAMP_DeInit(void)
{
HAL_OPAMP_DeInit(&hOPAMP);
}
void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef* hopamp)
{
if(hopamp->Instance==OPAMP1)
{
/* USER CODE BEGIN OPAMP1_MspDeInit 0 */
/* USER CODE END OPAMP1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_OPAMP_CLK_DISABLE();
/* USER CODE BEGIN OPAMP1_MspDeInit 1 */
/* USER CODE END OPAMP1_MspDeInit 1 */
}
}
|
void mx_opamp_hal_deinit(void)
{
(void)HAL_OPAMP_DeInit(&hOPAMP);
HAL_RCC_OPAMP1_Reset();
HAL_RCC_OPAMP1_DisableClock();
/* De-initialize all GPIO pins associated with OPAMP1 */
HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_6);
}
|